//
// Created by xili on 2024/8/29 9:44.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    vector <vector<int>> generateMatrix(int n) {
        vector <vector<int>> ans(n, vector<int>(n));
        int left = 0, right = n - 1;
        int top = 0, bot = n - 1;
        int cur = 1;
        while (left <= right && top <= bot) {
            for (int i = left; i <= right; i++) {
                ans[top][i] = cur++;
            }
            for (int i = top + 1; i <= bot; i++) {
                ans[i][right] = cur++;
            }
            if (left < right && top < bot) {
                for (int i = right - 1; i >= left; i--) {
                    ans[bot][i] = cur++;
                }
                for (int i = bot - 1; i >= top; i--) {
                    ans[i][top] = cur++;
                }
            }
            top++;
            bot--;
            left++;
            right--;
        }
        return ans;
    }
};